/**
 * 装车控制 - 硬件参数 - 物料参数设置 - 弹窗
 * @author xiaoxujs
 */

import { FC, useEffect, useState } from "react";
import { IIYckzZyLoadControlParamSetMaterial, IYckzZyLoadControlParamSet } from "../../../../../type/remoteControl/loadingControlList";
import { Form, Input, message, Modal } from "antd";
import { getYckzZyLoadControlMaterialNameUpdate } from "../../../api";


const WuliaoSetModule: FC<{
    open: boolean,
    handleSetCallback: () => void;
    clickData?: IYckzZyLoadControlParamSet,
    clickId?: string;
}> = ({
    open,
    handleSetCallback,
    clickData,
    clickId
}) => {

        const [form] = Form.useForm<{}>();

        const [loading, setLoading] = useState(false);

        useEffect(() => {
            if (clickData) {
                form.setFieldsValue(clickData)
            }
        }, [clickData, form])
        const handleOk = async () => {
            const values = await form.validateFields();
            if (clickData && clickId) {
                setLoading(true);
                const { code, msg } = await getYckzZyLoadControlMaterialNameUpdate({
                    ...values,
                    id: clickId
                }) as { code: number; msg: string };
                if (code === 20000) {
                    form.resetFields();
                    handleSetCallback();
                } else {
                    message.error(msg)
                }
                setLoading(false)

            }

        };

        const handleCancel = () => {
            handleSetCallback();
        };
        return <Modal title="设置物料参数" confirmLoading={loading} open={open} onOk={handleOk} onCancel={handleCancel}>
            <Form
                labelCol={{ span: 6 }}
                wrapperCol={{ span: 16 }}
                form={form}
            >
                <Form.Item<IIYckzZyLoadControlParamSetMaterial>
                    label="物料名称"
                    name="materialName"
                    rules={[{ required: true, message: '请输入物料名称!' }]}
                >
                    <Input />   
                </Form.Item>

            </Form>
        </Modal>
    }

export default WuliaoSetModule;